package com.phantasy.loaders.batchLoader
{
	import com.phantasy.caches.CacheURLLoader;
	import com.phantasy.loaders.Decoder;
	import com.phantasy.loaders.gameLoader.ResourceItem;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import org.osmf.layout.AbsoluteLayoutFacet;

	/**
	 *批加载器 
	 * @author Liseen
	 * 
	 */	
	public class BatchLoader extends EventDispatcher
	{
		public static var MAX_LOADER_COUNT:int = 5;
		//当前批处理长度		
		public var size:int;
		public var decoder:Decoder;
		public var loadCount:int = 0;
		
		private var _list:Array;
		private var _msg:String="";
		private var _isShow:Boolean=false;
		private var _isShowIng:Boolean=false;
		private var urlLoaders:Array;
		private var loadingLoader:Dictionary;
		public function BatchLoader()
		{
			urlLoaders=[];
			loadingLoader = new Dictionary();
			this.decoder = new Decoder();
			this.decoder.contentLoaderInfo.addEventListener(Event.COMPLETE, this.decoderComplete);
			this.decoder.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.decoderError);
			
			var cacheLoader:CacheURLLoader;
			
			while (this.urlLoaders.length < MAX_LOADER_COUNT)
			{
				cacheLoader = new CacheURLLoader();
				cacheLoader.dataFormat = URLLoaderDataFormat.BINARY;
				cacheLoader.addEventListener(Event.COMPLETE, this.completeHandler);
				cacheLoader.addEventListener(ProgressEvent.PROGRESS, this.onProgress);
				cacheLoader.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
				this.urlLoaders.push(cacheLoader);
			}
		}
		/**
		 *添加到批处理加载器中 
		 * @param url	路径
		 * @param data	数据
		 * 
		 */		
		public function add(url:String,data:Object=null):void
		{
			if(this._list==null){
				this._list=[];
			}
			
			if(this._msg==""){
				this._msg = data.toString();
			}
			
			var item:ResourceItem = new ResourceItem();
			item.url = url;
			item.data = data;
			this._list.push(item);
			this.size = this._list.length;
		}
		
		private function getLoader():CacheURLLoader
		{
			if (this.urlLoaders && this.urlLoaders.length > 0)
			{
				return (this.urlLoaders.shift());
			}
			return null;
		}
		
		private function completeHandler(_arg1:Event):void
		{
			var cacheLoader:CacheURLLoader = (_arg1.currentTarget as CacheURLLoader);
			var resourceItem:ResourceItem = this.loadingLoader[cacheLoader];
			delete this.loadingLoader[cacheLoader];
			this.urlLoaders.push(cacheLoader);
			if (resourceItem)
			{
				if ((((resourceItem.type == ResourceItem.SWF)) || ((resourceItem.type == ResourceItem.IMAGE))))
				{
					resourceItem.content = (cacheLoader.datas as ByteArray);
					this.decoder.add(resourceItem);
				}
				else
				{
					if (resourceItem.type == ResourceItem.FILE)
					{
						this.dispatch(BatchEvent.BATCH_ITEM_COMPLETE, resourceItem, cacheLoader.datas);
						this.loadCount++;
						if (this.loadCount == this.size)
						{
							this.loadOver();
							this.loadCount = 0;
							this.dispatchEvent(new BatchEvent(BatchEvent.BATCH_COMPLETE));
						}
					}
				}
			}
			this.load(this._isShow);
		}
		
		private function onProgress(_arg1:ProgressEvent):void
		{
			var _local4:Number;
			var _local5:Number;
			var _local2:CacheURLLoader = CacheURLLoader(_arg1.currentTarget);
			var _local3:ResourceItem = this.loadingLoader[_local2];
			if (_local3)
			{
				if ((this._isShow == true)&& (this._isShowIng == true))
				{
					_local4 = (_arg1.bytesLoaded / _arg1.bytesTotal);
					_local5 = ((this.loadCount + _local4) / this.size);
					//LoadingSetter.mapLoading(true, _local5, this.msg);
				}
				this.dispatch(BatchEvent.BATCH_ITEM_PROGRESS, _local3, {
					"bytesLoaded":_arg1.bytesLoaded,
					"bytesTotal":_arg1.bytesTotal
				})
			}
		}
		
		private function ioErrorHandler(_arg1:IOErrorEvent):void
		{
			var _local2:CacheURLLoader = CacheURLLoader(_arg1.currentTarget);
			var _local3:ResourceItem = this.loadingLoader[_local2];
			delete this.loadingLoader[_local2];
			this.urlLoaders.push(_local2);
			this.dispatch(BatchEvent.BATCH_ITEM_IO_ERROR, null, null);
			this.loadCount++;
			if (this.loadCount == this.size)
			{
				this.loadCount = 0;
				this.loadOver();
			}
		}
		
		private function loadOver():void
		{
			if (this._isShow)
			{
				//LoadingSetter.mapLoading(false);
			}
			this._isShow = (this._isShowIng = false);
			this._msg = "";
		}
		
		private function decoderComplete(e:Event):void
		{
			var decoder:Decoder = (e.target.loader as Decoder);
			var item:ResourceItem = decoder.info;
			try
			{
				this.dispatch(BatchEvent.BATCH_ITEM_COMPLETE, item, decoder.contentLoaderInfo.bytes);
				this.loadCount++;
				if (this.loadCount == this.size)
				{
					this.loadOver();
					this.loadCount = 0;
					this.dispatchEvent(new BatchEvent(BatchEvent.BATCH_COMPLETE));
				}
			}
			catch(e:Error)
			{
			}
			decoder.dispose();
		}
		
		private function decoderError(event:IOErrorEvent):void
		{
			this.decoder.dispose();
		}
		
		private function dispatch(type:String, item:ResourceItem, data:*):void
		{
			var batchEvent:BatchEvent = new BatchEvent(type);
			batchEvent.loadItem = item;
			batchEvent.data = data;
			dispatchEvent(batchEvent);
		}

		/**
		 *加载 
		 * @param isShow
		 * 
		 */		
		public function load(isShow:Boolean=false):void
		{
			var cacheURLLoader:CacheURLLoader;
			var resourceItem:ResourceItem;
			if(this._list&&(this._list.length>0)){
				if(this._isShow!=isShow){
					this._isShow = isShow;
				}
				
				if(isShow==true&&this._isShowIng==false){
					this._isShowIng=true;
					if(this._msg==""){
						this._msg = "正在加载资源";
					}
					
				}
				
				cacheURLLoader = getLoader();
				if(cacheURLLoader){
					resourceItem = this._list.shift();
					this.loadingLoader[cacheURLLoader] = resourceItem;
					cacheURLLoader.load(new URLRequest(resourceItem.url));
					this.load(this._isShow);
				}
			}
		}
	}
}